package com.m4f.web.controller;

import java.io.IOException;
import java.security.Principal;
import java.util.logging.Logger;

import javax.cache.CacheException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.m4f.business.service.AppConfigurationService;

@Controller
@RequestMapping(value="/login")
public class LoginController extends BaseController{

	private static final Logger LOGGER = Logger.getLogger(HomeController.class.getName());
	
	@Autowired
	private AppConfigurationService appConfigurationService;
	
	public LoginController() throws CacheException {
		super();
	}
	
	@RequestMapping(method=RequestMethod.GET)
	public String getView(Principal currentUser, Model model,
			@RequestParam(required=false, defaultValue="") String login_error)
			throws IOException, ClassNotFoundException {
		Long stage = this.appConfigurationService.getGlobalConfiguration().getStage();
		if(login_error!=null && !"".equals(login_error)) {
			String message = this.getMessage("user.login.error", null);
			model.addAttribute("message", message);
			return "common/error";
		}
		if(stage!=1) {
			return "login";
		}
		return "redirect:/";
	}
	
}